//
//  ExchangeTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/4/10.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "ExchangeTableViewCell.h"

@interface ExchangeTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * moneyImageView;
@property (nonatomic, weak)UILabel * moneyLabel;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * fenLabel;
@property (nonatomic, weak)UIButton * exchangeButton;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation ExchangeTableViewCell


-(void)setModel:(MoneyModel *)model{
    
    _model = model;
    
    int money = [model.book_value intValue];
    self.moneyLabel.text = [NSString stringWithFormat:@"%d元",money];
    self.titleLabel.text = model.name;
    self.fenLabel.text = [NSString stringWithFormat:@"%@积分",model.value];
    self.ID = [NSString stringWithFormat:@"%@",model.id];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    UIImageView * moneyImageView = [UIImageView new];
    
    moneyImageView.image = [UIImage imageNamed:@"现金红包"];
    
    self.moneyImageView = moneyImageView;
    
    [self.backView addSubview:self.moneyImageView];
    
    
    UILabel * moneyLabel = [UILabel new];
    //现金
    moneyLabel.textColor = ColorWithRGB(0xFFD426);
    moneyLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    
    self.moneyLabel = moneyLabel;
    
    [self.moneyImageView addSubview:self.moneyLabel];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ZRBlackColor;
    titleLabel.text = @"----";
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.backView addSubview:self.titleLabel];
    
    
    UILabel * fenLabel = [UILabel new];
    
    fenLabel.textColor = ColorWithRGB(0xFFD426);
    fenLabel.text = @"----";
    fenLabel.font = [UIFont boldSystemFontOfSize:13* SCREENH6];
    
    self.fenLabel = fenLabel;
    
    [self.backView addSubview:self.fenLabel];
    
    
    UIButton * exchangeButton = [UIButton new];
    
    [exchangeButton setTitle:@"兑换" forState:UIControlStateNormal];
    [exchangeButton setTitleColor:ColorWithRGB(0xFFD426) forState:UIControlStateNormal];
    exchangeButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    exchangeButton.layer.borderColor = [ColorWithRGB(0xFFD426) CGColor];
    exchangeButton.layer.borderWidth = 1.0f;
    exchangeButton.layer.cornerRadius = 5 * SCREENH6;
    exchangeButton.layer.masksToBounds = YES;
    [exchangeButton addTarget:self action:@selector(clickExchangeButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.exchangeButton = exchangeButton;
    
    [self.backView addSubview:self.exchangeButton];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
}


-(void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(90 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.moneyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.mas_equalTo(55 * SCREENH6);
        make.height.mas_equalTo(65 * SCREENH6);
    }];

    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.moneyImageView);
        make.top.mas_equalTo(self.moneyImageView).offset(35 * SCREENH6);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.moneyImageView).offset(15 * SCREENH6);
        make.leading.mas_equalTo(self.moneyImageView.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.fenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.exchangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.mas_equalTo(60 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.bottom.mas_equalTo(self.backView);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
}


- (void)clickExchangeButton{
    
    NSString * ID = [NSString stringWithFormat:@"%@",self.model.id];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickExchangeButtonID:)])
    {
        [self.delegate clickExchangeButtonID:ID];
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
